//
//  WXPPopDetainmentView.m
//  返回挽留View
//
//  Created by wxp on 2025/3/1.
//

#import "WXPPopDetainmentView.h"

@implementation WXPPopDetainmentView

#pragma mark - lazy
- (NSArray *)arrayTitle{
    if (!_arrayTitle) {
        _arrayTitle = @[
            @"Usted está a un paso para obtener el préstamo",
            @"Sólo 3 minutos para completar la autenticación",
            @"Está seguro de que no necesita el préstamo?"
        ];
    }
    return _arrayTitle;
}

- (UIView *)viewBack{
    if (_viewBack == nil) {
        _viewBack = [UIView new];
        _viewBack.backgroundColor = COLOR_WHITE;
        _viewBack.width = SCREEN_WIDTH-2*LEFT_MARDIN;
    }
    return _viewBack;
}
- (UILabel *)labelContent{
    if (_labelContent == nil) {
        _labelContent = [UILabel new];
        _labelContent.numberOfLines = 0;
        _labelContent.lineSpace = 3;
        _labelContent.textAlignment = NSTextAlignmentCenter;
        _labelContent.textColor = COLOR_BLACK;
        _labelContent.fontNum = 18;
    }
    return _labelContent;
}
- (UIButton *)btnConfirm{
    if (_btnConfirm == nil) {
        _btnConfirm = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnConfirm.backgroundColor = COLOR_PRIMARY;
        [_btnConfirm setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [_btnConfirm setTitle:@"Confirmar" forState:UIControlStateNormal];
        _btnConfirm.titleLabel.fontNum = 14;
        WEAKSELF
        [_btnConfirm setNoRepeatActionWithInterval:1 action:^(UIButton * _Nonnull button) {
            [NSObject clickFeedback];
            [weakSelf.btnConfirm addClickAnimate];
            [weakSelf removeFromSuperview];
        }];
    }
    return _btnConfirm;
}


#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.4];
        [self addSubView];
    }
    return self;
}

//添加subview
- (void)addSubView{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.labelContent];
    [self.viewBack addSubview:self.btnConfirm];
    [self resetViewWithTitle:@""];
}

#pragma mark - 刷新view
- (void)resetViewWithTitle:(NSString *)titleStr{
    int number = [[GlobalMethod readStrFromUserDefaults:LOCAL_DETAINMENT_NUM] intValue];
    NSString * showStr = self.arrayTitle[number%3];
    if(isStr(titleStr)){
        showStr = titleStr;
    }
    [self.labelContent fitTitle:showStr variable:self.viewBack.width-2*(22)];
    [GlobalMethod writeStr:[NSString stringWithFormat:@"%d",number+1] forKey:LOCAL_DETAINMENT_NUM];
    self.labelContent.centerXTop = XY(self.viewBack.width/2, Ratio(50));
    self.btnConfirm.widthHeight = XY(self.viewBack.width-30-30, BUTTON_HEIGHT);
    [GlobalMethod setRoundView:self.btnConfirm color:[UIColor clearColor] numRound:self.btnConfirm.height/2 width:0];
    self.btnConfirm.centerXTop = XY(self.viewBack.width/2, self.labelContent.bottom+BUTTON_HEIGHT);
    self.viewBack.height = self.btnConfirm.bottom+BUTTON_HEIGHT;
    self.viewBack.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    [GlobalMethod setRoundView:self.viewBack color:[UIColor clearColor] numRound:16 width:0];
}

@end
